﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Text;
using Gift.Model;
using Gift.Model.ENUM;
using Gift.DAL;


public partial class admin_leftMenu : System.Web.UI.Page
{
      readonly DALAdminLogin theAdminLogin = new DALAdminLogin();
        readonly DALRoleRuleRef theRoleRuleRef = new DALRoleRuleRef();
        readonly DALCompany theCompany = new DALCompany();
        readonly DALRule theRule = new DALRule();

        public StringBuilder SbTreeHtml = new StringBuilder();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                this.ValidateLoginRule();  
            }
        }

          void ValidateLoginRule()
        {

            SbTreeHtml.Append("<script language=\"javascript\" type=\"text/jscript\"> \n");
            SbTreeHtml.Append("ImgDir = \"leftimage/\";\n");
            SbTreeHtml.Append("SetFolderIcon(\"desktop.gif\");");
            SbTreeHtml.Append("treeMenuAddItem(0, \"&nbsp系统功能操作菜单\");\n");
            SbTreeHtml.Append("SetFolderIcon();\n");
            SbTreeHtml.Append("var MenuColor=top.MenuColor;\n");
            SbTreeHtml.Append("var MenuTextColor=\"#000000\";\n");

            int iAdminId = int.Parse(new BaseAdmin().Administrator.AdminUserId);

            Dictionary<string, MainMenu> dicMain = new Dictionary<string, MainMenu>();
              Dictionary<string, SubMainMenu> dicSubMain = new Dictionary<string, SubMainMenu>();
           
              MainMenu oMainMenu = new MainMenu();
              SubMainMenu oSubMainMenu = new SubMainMenu();
              tbCompany oCompany = theCompany.GetModelByAdminId(iAdminId);

              if (oCompany == null)
              {
                  DataSet dsRoleList = theRoleRuleRef.GetRoleListByAdminId(iAdminId);
                  if (dsRoleList != null && dsRoleList.Tables.Count > 0)
                      foreach (DataRow drRole in dsRoleList.Tables[0].Rows)
                      {
                          int iRoleId = int.Parse(drRole["id"].ToString());
                          DataSet dsSub = theRoleRuleRef.GetMainListByRoleId(iRoleId);
                          DataSet ds1 = theRoleRuleRef.GetListByRoleId(iRoleId);
                          if (dsSub != null && dsSub.Tables.Count > 0 && ds1 != null && ds1.Tables.Count > 0)
                          {
                              foreach (DataRow drSub in dsSub.Tables[0].Rows)
                              {
                                  if (!dicMain.ContainsKey(drSub["Description"].ToString()))
                                  {
                                      oMainMenu = new MainMenu();
                                      oMainMenu.Id = drSub["RuleId"].ToString();
                                      oMainMenu.Name = drSub["Description"].ToString();
                                      oMainMenu.NavUrl = drSub["Url"].ToString();
                                      dicMain.Add(oMainMenu.Name, oMainMenu);
                                  }

                                  foreach (DataRow dr in ds1.Tables[0].Rows)
                                  {

                                      if (drSub["RuleId"].ToString() == dr["ParentId"].ToString())
                                      {
                                          if (dr["Url"] != null && dr["Url"].ToString() != string.Empty)
                                          {
                                              if (!dicSubMain.ContainsKey(dr["Description"].ToString()))
                                              {
                                                  oSubMainMenu = new SubMainMenu();
                                                  oSubMainMenu.Id = dr["RuleId"].ToString();
                                                  oSubMainMenu.Name = dr["Description"].ToString();
                                                  oSubMainMenu.ParentId = drSub["RuleId"].ToString();
                                                  oSubMainMenu.NavUrl = dr["Url"].ToString();
                                                  dicSubMain.Add(oSubMainMenu.Name, oSubMainMenu);
                                              }

                                          }
                                      }
                                  }
                              }
                          }
                      }
              }
             
              else 
              {
                  if (oCompany.IsDelete)
                  {
                      base.Response.Write("<script language=javascript>alert('贵公司账号已被禁用，如使用请与客服联系！');window.top.location.href='login.aspx';</script>");
                      base.Response.End();
                  }

                  DataSet dsMain = theRoleRuleRef.GetMainListByCompanyIdForCompany(oCompany.Id);

                  DataSet ds0 = theRoleRuleRef.GetListByCompanyIdForCompany(oCompany.Id);
                  if (dsMain != null && dsMain.Tables.Count > 0 && ds0 != null && ds0.Tables.Count > 0)
                  {

                      foreach (DataRow drMain in dsMain.Tables[0].Rows)
                      {
                          if (!dicMain.ContainsKey(drMain["Description"].ToString()))
                          {
                              oMainMenu = new MainMenu();
                              oMainMenu.IsCompany = true;
                              oMainMenu.Id = drMain["RuleId"].ToString();
                              oMainMenu.Name = drMain["Description"].ToString();
                              oMainMenu.NavUrl = drMain["Url"].ToString();
                              dicMain.Add(oMainMenu.Name, oMainMenu);
                          }

                          foreach (DataRow dr in ds0.Tables[0].Rows)
                          {
                              if (drMain["RuleId"].ToString() == dr["ParentId"].ToString())
                              {
                                  if (dr["Url"] != null && dr["Url"].ToString() != string.Empty)
                                  {
                                      if (!dicSubMain.ContainsKey(dr["Description"].ToString()))
                                      {
                                          oSubMainMenu = new SubMainMenu();
                                          oSubMainMenu.IsCompany = true;
                                          oSubMainMenu.Id = dr["RuleId"].ToString();
                                          oSubMainMenu.Name = dr["Description"].ToString();
                                          oSubMainMenu.ParentId = drMain["RuleId"].ToString();
                                          oSubMainMenu.NavUrl = dr["Url"].ToString();
                                          dicSubMain.Add(oSubMainMenu.Name, oSubMainMenu);
                                      }

                                  }
                              }
                          }
                      }
                  }
              }

              foreach (MainMenu menu in dicMain.Values)
              {
                  SbTreeHtml.Append("treeMenuAddItem(1, \"" + menu.Name + "\");\n");
                  foreach (SubMainMenu subMenu in dicSubMain.Values)
                  {
                      if (subMenu.ParentId == menu.Id && subMenu.IsCompany == menu.IsCompany)
                      {
                          string sDescription = subMenu.Name.Length > 5 ? subMenu.Name.Replace("列表", "") : subMenu.Name;
                          SbTreeHtml.Append("treeMenuAddItem(2, \"" + sDescription + "\", \"" + subMenu.NavUrl + "\", \"tabWin\",\"l4.gif\");\n");
                      }
                  }
 
              }
            SbTreeHtml.Append("treeMenuAddItem(1, \"个人设置\");\n");
            SbTreeHtml.Append("treeMenuAddItem(2, \"修改密码\", \"system/setPassword.aspx\", \"tabWin\",\"l4.gif\");\n");
            SbTreeHtml.Append("</script>\n");
            SbTreeHtml.Append("<script language=\"JavaScript\" type=\"text/jscript\">\n");
            SbTreeHtml.Append("initializeDocument();\n");
            SbTreeHtml.Append("clickOnNode(0);\n");
            SbTreeHtml.Append("</script>\n");
        }
    }

public class MainMenu
{
    string _id = string.Empty;

    public string Id
    {
        get { return _id; }
        set { _id = value; }
    }
    bool _isCompany = false;

    public bool IsCompany
    {
        get { return _isCompany; }
        set { _isCompany = value; }
    }
    string _name = string.Empty;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    string _navUrl = string.Empty;

    public string NavUrl
    {
        get { return _navUrl; }
        set { _navUrl = value; }
    }
}

public class SubMainMenu
{
    string _id = string.Empty;

    public string Id
    {
        get { return _id; }
        set { _id = value; }
    }

    bool _isCompany = false;

    public bool IsCompany
    {
        get { return _isCompany; }
        set { _isCompany = value; }
    }

    string _name = string.Empty;

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
    string _navUrl = string.Empty;

    public string NavUrl
    {
        get { return _navUrl; }
        set { _navUrl = value; }
    }
    string _parentId = string.Empty;

    public string ParentId
    {
        get { return _parentId; }
        set { _parentId = value; }
    }
}

